/*
 * @description  : 离线缓存数据 数据库表操作API 封装
 * @descriptionDetail: 离线缓存数据 数据库表操作API 封装
 * @copyright    : 浙江烟草
 * @author       : dw
 * @create       : 2022-10-09 22:21:21
 */
import webDB from '@/common/database.js';
import {
  keySearchInfo,
} from './sql.js';
import {
  v4 as uuidv4,
} from 'uuid';

// 一键搜
const keySearchService = {
  // 保存历史数据
  savekeySearchInfoDb: function(list) {
    if (!Array.isArray(list) || list.length == 0) {
      resolve();
      return;
    }
    let arr = [];
    let apt = [];
    for (let i = 0; i < list.length; i++) {
      arr.push(
        [
          keySearchInfo.insertSQL,
          [
            list[i].userId,
            list[i].uuid,
            list[i].title,
            list[i].context,
            list[i].type,
          ],
        ]
      );
      apt.push({
        cho: keySearchInfo.createSQL_H5,
        val: [
          list[i].userId,
          list[i].uuid,
          list[i].title,
          list[i].context,
          list[i].type,
        ],
      });
    }
    return webDB.insertDataBatch(arr, apt);
  },
  //  删除所有缓存数据
  clearkeySearchInfoDb: function() {
    let ar = [keySearchInfo.deleteSQL,];
    let ap = [keySearchInfo.createSQL_H5,];
    return webDB.deleteDataBatch(ar, ap);
  },
  // 获取所有数据
  getAllkeySearchInfoDb: function() {
    return webDB.findAllData(keySearchInfo.queryAllSQL, keySearchInfo.createSQL_H5.name);
  },
  //  根据userId、type获取历史数据
  getkeySearchInfoByCode: function(userId, code) {
    return webDB.findData(keySearchInfo.queryWhereSQL, [userId, code,],
      keySearchInfo.createSQL_H5, keySearchInfo.createSQL_H5.queryKeys);
  },
}

module.exports = {
  keySearchService,
}
